//
//  BLCloseChaneViewController.m
//  blanket
//
//  Created by Mac on 2017/12/6.
//  Copyright © 2017年 stoneobs@icloud.com. All rights reserved.
//

#import "BLCloseChaneViewController.h"
#import "BLClothAddPresntViewController.h"
#import "BLRepertoryModel.h"
@interface BLCloseChaneViewController ()
@property(nonatomic, strong) STLabel                     *percetLabel;
@property(nonatomic, strong) STLabel                     *desLabel;
@property(nonatomic, strong) BLRepertoryModel                     *model;
@property(nonatomic, strong) NSMutableArray                     *labelArray;
@property(nonatomic, strong) STNoresultView                     *noresultView;
@end

@implementation BLCloseChaneViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = _isClothAdd?@"布草增加":@"布草减少";
    self.tableView.backgroundColor = [UIColor whiteColor];
    [self configSubView];
    [self sendMyRepertoryRequest];
//    self.noresultView = [[STNoresultView alloc] initWithFrame:CGRectMake(0, 100, UIScreenWidth, 300)
//                                                        title:@"抱歉,暂无库存数据"
//                                                  buttonTitle:@"增加库存"
//                                                 buttonHandle:^(NSString *titleString) {
//                                                     // [weakSelf onSelectedCommitButton];
//                                                 }];
//    self.noresultView.hidden = YES;
//    [self.tableView addSubview:self.noresultView];
    
    // Do any additional setup after loading the view.
}

#pragma mark --subView
- (void)configSubView{
    UIView * header = [[UIView alloc] initWithFrame:CGRectMake(0, 0, UIScreenWidth, 100)];
    header.backgroundColor = [UIColor whiteColor];
    
    UIView * topView = [[UIView alloc] initWithFrame:CGRectMake(30, 30, UIScreenWidth - 60, 90)];
    topView.backgroundColor = BL_AlertColor;
    topView.layer.cornerRadius = 5;
    topView.clipsToBounds = YES;
    [header addSubview:topView];
    self.percetLabel = [[STLabel alloc] initWithFrame:CGRectMake(5, 5, topView.width - 10, 20)
                                                 text:@"当前闲置布草占比46%"
                                            textColor:[UIColor whiteColor]
                                                 font:16
                                          isSizetoFit:NO
                                        textAlignment:NSTextAlignmentCenter];
    [topView addSubview:self.percetLabel];
    
    self.percetLabel.text = self.percetAlert;
    
    self.desLabel = [[STLabel alloc] initWithFrame:CGRectMake(0, self.percetLabel.bottom + 10, 70, 25)
                                              text:@"正常"
                                         textColor:BL_AlertColor
                                              font:13
                                       isSizetoFit:NO
                                     textAlignment:NSTextAlignmentCenter];
    self.desLabel.layer.cornerRadius = 3;
    self.desLabel.backgroundColor = [UIColor whiteColor];
    self.desLabel.clipsToBounds = YES;
    self.desLabel.centerX = topView.width / 2;
    self.desLabel.textColor = self.desColror;
    self.desLabel.text = self.desAlert;
    [topView addSubview:self.desLabel];
    
    STLabel * titleLable = [[STLabel alloc] initWithFrame:CGRectMake(0, self.desLabel.bottom + 10, 200, 20)
                                                     text:@"《温馨提示》"
                                                textColor:[UIColor whiteColor]
                                                     font:12
                                              isSizetoFit:NO
                                            textAlignment:NSTextAlignmentCenter];
    titleLable.bottom = topView.height;
    titleLable.centerX = topView.width / 2;
    [topView addSubview:titleLable];
    
    STLabel * lable = [[STLabel alloc] initWithFrame:CGRectMake(30, topView.bottom + 30, UIScreenWidth - 60, 40)
                                                text:@"当前库存:"
                                           textColor:BL_firstTextColor
                                                font:15
                                         isSizetoFit:NO
                                       textAlignment:NSTextAlignmentLeft];
    [header addSubview:lable];
    [self cofigAllGoodWithTop:lable.bottom superView:header];
    [self confiTableFooter];
    
}
- (void)confiTableFooter{
    __weak typeof(self) weakSelf =  self;
    UIView * footer = [[UIView alloc] initWithFrame:CGRectMake(0, 0, UIScreenWidth, 100)];
    footer.backgroundColor = self.tableView.backgroundColor;
    STButton * confimButton = [[STButton alloc] initWithFrame:CGRectMake(30, 0, UIScreenWidth - 60, 44)
                                                        title:_isClothAdd?@"增加库存":@"减少库存"
                                                   titleColor:[UIColor whiteColor]
                                                    titleFont:18
                                                 cornerRadius:5
                                              backgroundColor:BL_BlueBackGroundColor
                                              backgroundImage:nil
                                                        image:nil];
    [confimButton setClicAction:^(UIButton *sender) {
        [weakSelf onSelectedCommitButton];
    }];
    confimButton.st_centerY = 50;
    confimButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    [footer addSubview:confimButton];
    self.tableView.tableFooterView = footer;
}
- (void)cofigAllGoodWithTop:(CGFloat)top superView:(UIView*)superView{
    CGFloat labelWith = UIScreenWidth/2 - 45;
    self.labelArray = [NSMutableArray new];
    NSArray * array = @[@"床单1.8",@"被套1.8",@"床单1.5",@"被套1.5",@"床单1.2",@"被套1.2",@"枕套",@"面巾",@"浴巾"];
    for (int i = 0; i < 9; i ++) {
        NSString * title = array[i];
        STLabel * titleLable = [[STLabel alloc] initWithFrame:CGRectMake(30 * (i%2) + 30 + labelWith * (i%2), top + 50 * (i/2), labelWith, 40)
                                                         text:[NSString stringWithFormat:@"  %@:0",title]
                                                    textColor:BL_firstTextColor
                                                         font:14
                                                  isSizetoFit:NO
                                                textAlignment:NSTextAlignmentLeft];
        titleLable.backgroundColor = BL_backgroundColor;
        [superView addSubview:titleLable];
        [self.labelArray addObject:titleLable];
        superView.height = titleLable.bottom;
    }
    self.tableView.tableHeaderView = superView;
}
- (void)onSelectedCommitButton{
    
    BLClothAddPresntViewController * vc = [BLClothAddPresntViewController new];
    vc.isClothAdd = self.isClothAdd;
    if (!vc.isClothAdd && !self.model) {
        [SVProgressHUD showErrorWithStatus:@"没有库存，无法减少，请先增加库存"];
        return;
    }
    vc.originModel = self.model;
    [vc setFinshActionHandle:^(NSDictionary *dic,UIViewController* vc) {
        [self sendAddOrReduceRequestwithDic:dic vc:vc];
    }];
    [self presentViewController:vc animated:NO completion:nil];
    
}
- (void)reloadData{

    if (self.labelArray.count != self.model.stkeyArray.count) {
       // [SVProgressHUD showErrorWithStatus:@"不能增加字段"];
        return;
    }
    NSArray * stkeyArray = self.model.stkeyArray;
    NSMutableArray * finshArray = [NSMutableArray arrayWithCapacity:9];
    for (NSDictionary * dic in stkeyArray) {
        NSString * name = dic[@"BLRepertoryModelTitleKey"];
        if ([name containsString:@"床1.8"]) {
            [finshArray addObject:dic];
        }
    }
    for (NSDictionary * dic in stkeyArray) {
        NSString * name = dic[@"BLRepertoryModelTitleKey"];
        if ([name containsString:@"被子1.8"]) {
            [finshArray addObject:dic];
        }
    }
    for (NSDictionary * dic in stkeyArray) {
        NSString * name = dic[@"BLRepertoryModelTitleKey"];
        if ([name containsString:@"床1.5"]) {
            [finshArray addObject:dic];
        }
    }
    for (NSDictionary * dic in stkeyArray) {
        NSString * name = dic[@"BLRepertoryModelTitleKey"];
        if ([name containsString:@"被子1.5"]) {
            [finshArray addObject:dic];
        }
    }
    for (NSDictionary * dic in stkeyArray) {
        NSString * name = dic[@"BLRepertoryModelTitleKey"];
        if ([name containsString:@"床1.2"]) {
            [finshArray addObject:dic];
        }
    }
    for (NSDictionary * dic in stkeyArray) {
        NSString * name = dic[@"BLRepertoryModelTitleKey"];
        if ([name containsString:@"被子1.2"]) {
            [finshArray addObject:dic];
        }
    }
    for (NSDictionary * dic in stkeyArray) {
        NSString * name = dic[@"BLRepertoryModelTitleKey"];
        if ([name containsString:@"枕套"]) {
            [finshArray addObject:dic];
        }
    }
    for (NSDictionary * dic in stkeyArray) {
        NSString * name = dic[@"BLRepertoryModelTitleKey"];
        if ([name containsString:@"面巾"]) {
            [finshArray addObject:dic];
        }
    }
    for (NSDictionary * dic in stkeyArray) {
        NSString * name = dic[@"BLRepertoryModelTitleKey"];
        if ([name containsString:@"浴巾"]) {
            [finshArray addObject:dic];
        }
    }
    for (int i = 0; i < self.labelArray.count; i ++ ) {
        UILabel * label = self.labelArray[i];
        NSDictionary * dic = finshArray[i];
        NSString * title = dic[BLRepertoryModelTitleKey];
        NSString * num = dic[BLRepertoryModelNumKey];
        label.text = [NSString stringWithFormat:@" %@:%@",title,num];
    }
    
    self.noresultView.hidden = self.model?YES:NO;
}
#pragma mark --NetWork Method
- (void)sendMyRepertoryRequest{
    [SVProgressHUD showWithStatus:@"请稍后"];
    NSMutableDictionary * dic = [NSMutableDictionary new];
    [dic setObject:debugToken forKey:@"token"];
    NSString * url = [NSString stringWithFormat:@"%@%@",releaseServerUrlHeader,@"index.php/Api/Basics/currentStockAction"];
    if ([STUserManger defult].userID.length) {
        [dic setObject:[STUserManger defult].userID forKey:@"uid"];
    }

    [[STNetWrokManger defaultClient] requestWithPath:url
                                              method:STHttpRequestTypePost
                                          parameters:dic
                                             success:^(NSURLSessionDataTask *operation, id responseObject) {
                                                 [self.tableView.mj_header endRefreshing];
                                                 [SVProgressHUD dismiss];
                                                 NSDictionary * dataDic =  responseObject[@"data"][@"current"];
                                                 if (![dataDic isKindOfClass:[NSNull class]]) {
                                                     self.model = [BLRepertoryModel mj_objectWithKeyValues:dataDic];
                                                     self.model.originDic = dataDic;
                                                     [self reloadData];
                                                 }
                                                 //[self configSubView];

                                                 
                                             } failure:^(NSString *stateCode, STError *error) {
                                                 [self.tableView.mj_header endRefreshing];
                                                 [self reloadData];
                                                 [SVProgressHUD dismiss];
                                                 [SVProgressHUD showErrorWithStatus:@"暂无库存，请增加库存"];
                                             }];
}
- (void)sendAddOrReduceRequestwithDic:(NSDictionary*)dic vc:(UIViewController*)vc{
    [SVProgressHUD showWithStatus:@"请稍后"];
    NSString * url = [NSString stringWithFormat:@"%@%@",releaseServerUrlHeader,@"index.php/Api/Basics/addOrReduceStock"];
    [[STNetWrokManger defaultClient] requestWithPath:url
                                              method:STHttpRequestTypePost
                                          parameters:dic
                                             success:^(NSURLSessionDataTask *operation, id responseObject) {
                                                 [self.tableView.mj_header endRefreshing];
                                                 [SVProgressHUD dismiss];
                                                 NSString * aletrStr = self.isClothAdd?@"增加布草成功": @"减少布草成功";
                                                 [vc dismissViewControllerAnimated:NO completion:nil];
                                                 [SVProgressHUD showSuccessWithStatus:aletrStr];
                                                 dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                                                     [self sendMyRepertoryRequest];
                                                 });
                                                 
                                                 
                                             } failure:^(NSString *stateCode, STError *error) {
                                                 [self.tableView.mj_header endRefreshing];
                                                 [SVProgressHUD dismiss];
                                                 [SVProgressHUD showErrorWithStatus:error.desc];
                                             }];
}
@end

